package com.archgeek.java.conc.lock;

import java.util.concurrent.Semaphore;

/**
 * @author pizhihui
 * @date 2023-04-20 17:52
 */
public class SemaphoreTest {

    public static void main(String[] args) {

        Semaphore semaphore = new Semaphore(2, false); // 构造函数入参，permits：信号量、fair：公平锁/非公平锁
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName() + "蹲坑");
                    Thread.sleep(1000L);
                } catch (InterruptedException ignore) {
                } finally {
                    semaphore.release();
                }
            }, "蹲坑编号：" + i).start();
        }

    }

}
